<template>
  <div>
   <div class="addcategory">
    <el-form ref="ruleForm" :model="ruleForm" label-width="80px" :rules="rules">
      <el-form-item label="上级分类" prop="category">
        <el-select v-model="ruleForm.category" placeholder="请选择分类">
          <el-option
            :label="item.name"
            :value="item.id"
            v-for="item in category"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="ruleForm.name" width="80px"></el-input>
      </el-form-item>
      <el-form-item label="分类图片">
        <el-upload
          class="avatar-uploader"
          action="http://kumanxuan1.f3322.net:8360/admin/upload/goodNewPic"
          :headers="headers"
          name="good_pic"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" width="100%" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="显示">
        <el-switch
          v-model="ruleForm.show"
          active-value="1"
          inactive-value="0"
          active-color="#409eff"
          inactive-color="#dcdfe6"
        >
        </el-switch>
      </el-form-item>
      <el-form-item label="排序">
        <el-input v-model="ruleForm.sort" width="80px"></el-input>
      </el-form-item>
      <el-form-item label="分类描述" prop="desc">
        <el-input
          type="textarea"
          v-model="ruleForm.desc"
          width="80px"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="baocunFn">保存</el-button
        ><el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>
  </div>
</template>

<script>
const {getTopCategory,getCategoryDetail,addCategory} = require("../../request/httpApi")
export default {
  data(){
    return{
      category:[],
       headers: {
        "X-Nideshop-Token": localStorage.getItem("token"),
      },
      imageUrl:"",
      ruleForm:{
        category: "",
        sort: "",
        desc: "",
        name: "",
        show: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 2, max: 10, message: "长度在 2 到 10 个字符", trigger: "blur" },
        ],
        category: [
          { required: true, message: "请输入上级分类", trigger: "blur" },
        ],
        desc: [
          { required: true, message: "请输入分类描述", trigger: "blur" },
          { min: 10, max: 1000, message: "长度在 10 到 1000 个字符", trigger: "blur" },
        ],
      },
    }
  },

  //发送请求
  created(){
    console.log(this.$route.params.cid);
     //获取分类数据
    getTopCategory().then((res) => {
      this.category = res.data
    });

    //获取单个分类的详情 渲染编辑点击的视图
    getCategoryDetail({
      id:this.$route.params.cid
    }).then( res=> {
      if(res.errno===0){
        // console.log(res.data);
        this.ruleForm.category=  res.data.level==='L1'?'顶级分类':res.data.parent_id,
        this.ruleForm.sort=res.data.sort_order,
        this.ruleForm.desc=res.data.front_name,
        this.ruleForm.name=res.data.name,
        this.ruleForm.show=res.data.is_show.toString(),
        this.imageUrl = res.data.wap_banner_url
      }
    })


  
  },
     //这里是写方法的地方
    methods:{
         handleAvatarSuccess(file) {
      // console.log(file.data.fileUrl);
      this.imageUrl = file.data.fileUrl;
    },
    //上传图片限制大小的函数
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt50K = file.size / 1024 / 1024 / 1024 < 50;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt50K) {
        this.$message.error("上传头像图片大小不能超过 50K!");
      }
      return isJPG && isLt50K;
    },
    //点击保存按钮
    baocunFn() {
      //判断如果是空 不给发送请求
      if (
        this.ruleForm.desc === "" ||
        this.ruleForm.name === "" ||
        this.ruleForm.category === ""
      ) {
        this.$message.error("内容不能为空");
        return;
      } else {
          addCategory({
          id:this.$route.params.cid,
          front_desc: this.ruleForm.desc,
          name: this.ruleForm.name,
          is_show: this.ruleForm.show,
          front_name: this.ruleForm.category,
          banner_url: this.imageUrl,
        }).then((res) => {
          if (res.errno === 0) {
            this.$route.params.cid?this.$message.success("修改成功"):this.$message.success("添加成功");
            // console.log(res);
            this.$router.push('/category')
          }
        });
      }
    },
    },
}
</script>

<style lang="less">
  .addcategory {
  background-color: #fff;
  padding: 10px;
  padding-left: 20px;
  .el-input {
    width: 225px;
  }
  .el-textarea {
    width: 225px;
  }
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 105px;
  height: 105px;
  line-height: 105px;
  text-align: center;
  border: 1px solid #8c939d;
}
.avatar {
  width: 105px;
  height: 105px;
  display: block;
}
</style>